package com.leetcode;

/**
 * 27. 移除元素
 *
 * @author fy
 * @date 2022/4/1 21:18
 */
public class Solution27 {

    public int removeElement(int[] nums, int val) {
        int n = nums.length;
        int count = 0;
        for (int i = 0, j = n - 1; i <= j;) {
            if (nums[i] == val) {
                count++;
                nums[i] = nums[j--];
            } else {
                i++;
            }
        }
        return n - count;
    }

    public int removeElement1(int[] nums, int val) {
        int n = nums.length;
        int count = 0;
        for (int i = 0, j = 0; i < n; i++) {
            if (nums[i] != val) {
                nums[j++] = nums[i];
            } else {
                count++;
            }
        }
        return n - count;
    }

    public static void main(String[] args) {

    }

}
